package com.douqu.game.main.util;

import com.bean.core.util.TimeUtils;
import com.douqu.game.core.config.FundRewardConfig;
import com.douqu.game.core.config.RechargeConfig;
import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.controller.PlayerController;
import com.douqu.game.core.database.model.PlayerModel;
import com.douqu.game.core.e.E_RechargeGoodsType;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.common.TwoTuple;
import com.douqu.game.core.entity.db.MailDB;
import com.douqu.game.core.entity.ext.*;
import com.douqu.game.core.entity.ext.data.boon.AuctionRewardPlayer;
import com.douqu.game.core.entity.ext.data.boon.BonusData;
import com.douqu.game.core.entity.ext.data.boon.RechargeRecordData;
import com.douqu.game.core.entity.world.WorldArenaData;
import com.douqu.game.core.entity.world.WorldRankData;
import com.douqu.game.core.factory.ConfigFactory;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.WorldInfoFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.main.GameServer;
import com.douqu.game.main.SpringServer;
import com.douqu.game.main.database.mapper.read.ReadPlayerMapper;
import com.douqu.game.main.database.mapper.read.ReadServerMapper;
import com.douqu.game.main.database.mapper.write.WriteServerMapper;
import com.douqu.game.main.server.LoadManager;
import com.douqu.game.main.server.SpringContext;
import com.douqu.game.main.service.PlayerService;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

//import com.douqu.game.core.entity.db.InstanceDB;
//import com.douqu.game.core.entity.ext.data.challenge.InstanceData;

/**
 * 修正新增数据
 * @Author: Bean
 * @Description:
 * @Date: 2017-12-01 17:43
 */
public class FixDBUtils {


    public static void main(String[] args)
    {
        String type = args.length == 0 ? "reloadWorld" : args[0];
        if("fixPlayer".equals(type))
        {
            fixPlayerDB();
        }
        else if("fixWorld".equals(type))
        {
            fixWorldDB();
        }
        else if("fixAll".equals(type))
        {
            fixAllDB();
        }
        else if("reloadPlayer".equals(type))
        {
            reloadPlayer();
        }
        else if("reloadWorld".equals(type))
        {
            reloadWorldInfo();
        }
        else if("reloadAll".equals(type))
        {
            reloadAll();
        }
        else if("fixMergeFund".equals(type))
        {
            fixMergeFund();
        }
        else if("fixZbs".equals(type))
        {
            fixZbs();
        }
        else
        {
            System.out.println("类型错误 -> " + type);
        }

        System.exit(0);
    }

    private static void fixZbs()
    {
        LogUtils.info("开始清理争霸赛----------------");

        SpringServer.start();

        LoadManager.GMLoad();

        ReadPlayerMapper playerMapper = SpringContext.getBean(ReadPlayerMapper.class);

        List<Player> playerList = loadPlayer(playerMapper);

        for(Player player : playerList)
        {
            player.getWorldMapKillData().init();
        }

        LogUtils.info("修复成功,现在开始存储数据,玩家数量 -> " + playerList.size());

        JdbcTemplate jdbcTemplate = SpringContext.getBean(JdbcTemplate.class);

        System.out.println("1");

        TimeUtils.sleep(1000);

        System.out.println("2");

        TimeUtils.sleep(1000);

        System.out.println("3");

        savePlayer(jdbcTemplate, playerList);

        LogUtils.info("数据存储完成 -> 玩家数量:" + playerList.size());

        System.exit(0);
    }
    private static void fixRank()
    {
        SpringServer.start();

        LoadManager.GMLoad();

        ReadServerMapper readServerMapper = SpringContext.getBean(ReadServerMapper.class);
        WorldInfoFactory.getInstance().load(readServerMapper.find());

        WorldRankData worldRankData = WorldInfoFactory.getInstance().getWorldRankData();
        worldRankData.clear(SGCommonProto.E_RANK_TYPE.RANK_TYPE_INSTANCE.getNumber());

        WriteServerMapper writeServerMapper = SpringContext.getBean(WriteServerMapper.class);
        writeServerMapper.update(WorldInfoFactory.getInstance().save());

        System.exit(0);
    }

    /**
     * 处理合区后的开服基金
     */
    private static void fixMergeFund()
    {
        LogUtils.info("开始处理新区和开服基金----------------");

        SpringServer.start();

        LoadManager.GMLoad();

        ReadPlayerMapper playerMapper = SpringContext.getBean(ReadPlayerMapper.class);

        List<Player> playerList = loadPlayer(playerMapper);

        for(Player player : playerList)
        {
            fixMergeArea(player);
        }

        LogUtils.info("修复成功,现在开始存储数据,玩家数量 -> " + playerList.size());

        JdbcTemplate jdbcTemplate = SpringContext.getBean(JdbcTemplate.class);

        System.out.println("1");

        TimeUtils.sleep(1000);

        System.out.println("2");

        TimeUtils.sleep(1000);

        System.out.println("3");

        savePlayer(jdbcTemplate, playerList);

        LogUtils.info("数据存储完成 -> 玩家数量:" + playerList.size());

        System.exit(0);
    }

    private static void fixRobot()
    {
        SpringServer.start();

        LoadManager.GMLoad();

        ReadPlayerMapper playerMapper = SpringContext.getBean(ReadPlayerMapper.class);

        List<Player> playerList = loadRobot(playerMapper);

        System.out.println("加载机器人信息成功, 数量 -> " + playerList.size() + " 休息3秒......");

        System.out.println("1");

        TimeUtils.sleep(1000);

        System.out.println("2");

        TimeUtils.sleep(1000);

        System.out.println("3");

        System.out.println("写入机器人信息完成");

        JdbcTemplate jdbcTemplate = SpringContext.getBean(JdbcTemplate.class);

        savePlayer(jdbcTemplate, playerList);

        System.out.println("写入玩家信息完成!");

    }

    private static void fixCampBattle()
    {
        SpringServer.start();

        LoadManager.GMLoad();

        ReadPlayerMapper playerMapper = SpringContext.getBean(ReadPlayerMapper.class);

        List<Player> playerList = loadPlayer(playerMapper);
        for(Player player : playerList)
        {
            if(player.getCampBattleData().curTotalExploit > 0)
                LogUtils.info("玩家 -> " + player.name + " 领奖 -> " + player.getCampBattleData().isGetRewarded() + " index -> " + player.getCampBattleData().activityIndex + " 报名次数 -> " + player.getCampBattleData().applyCount + " 复活次数 -> " + player.getCampBattleData().reviveCount + " 累计功勋 -> " + player.getCampBattleData().curTotalExploit);
            player.getCampBattleData().clearAll();
        }

        PlayerService playerService = SpringContext.getBean(PlayerService.class);
        playerService.updatePlayers(playerList, false, ChallengeInfo.class);

        System.exit(0);
    }


    private static void fixOfficial()
    {
        SpringServer.start();

        LoadManager.GMLoad();

        ReadServerMapper readServerMapper = SpringContext.getBean(ReadServerMapper.class);
        WorldInfoFactory.getInstance().load(readServerMapper.find());

        WorldInfoFactory.getInstance().getWorldOfficialData().init();

        WriteServerMapper writeServerMapper = SpringContext.getBean(WriteServerMapper.class);
        writeServerMapper.update(WorldInfoFactory.getInstance().save());

        System.exit(0);
    }


    private static void fixArena()
    {
        SpringServer.start();

        LoadManager.GMLoad();

        ReadPlayerMapper playerMapper = SpringContext.getBean(ReadPlayerMapper.class);

        List<PlayerModel> robotList = playerMapper.findAllRobot();

        ReadServerMapper readServerMapper = SpringContext.getBean(ReadServerMapper.class);
        WorldInfoFactory.getInstance().load(readServerMapper.find());

        WorldArenaData worldArenaData = WorldInfoFactory.getInstance().getArenaRankInfo();
        WorldRankData worldRankData = WorldInfoFactory.getInstance().getWorldRankData();
        worldArenaData.clear();
        for(PlayerModel robot : robotList)
        {
            worldRankData.updateRankInfo(SGCommonProto.E_RANK_TYPE.RANK_TYPE_FC, robot.getObjectIndex(), robot.getFc());

            worldArenaData.getArenaRankByObjectIndex(robot.getObjectIndex());
        }


        System.out.println("竞技排行榜->");
//        int i = 0;
//        List<WorldArenaBean> arenaList = worldArenaData.getUnderTargetRankList(100);
//        for(WorldArenaBean worldArenaBean : arenaList)
//        {
//            for(PlayerModel robot : robotList)
//            {
//                if(robot.getObjectIndex().equals(worldArenaBean.getObjectIndex()))
//                {
//                    System.out.println((++i) + " 名字->" + playerModel.getName() + " 等级->" + playerModel.getLevel() + " 战力->" + playerModel.getFc());
//                    break;
//                }
//            }
//        }
//
//        System.out.println("战力排行榜->");
//        i = 0;
//        List<WorldRankBean> rankList = worldRankData.getRankList(SGCommonProto.E_RANK_TYPE.RANK_TYPE_FC);
//        for(WorldRankBean worldRankBean : rankList)
//        {
//            for(PlayerModel playerModel : playerList)
//            {
//                if(playerModel.getObjectIndex().equals(worldRankBean.getObjectIndex()))
//                {
//                    System.out.println((++i) + "名字->" + playerModel.getName() + " 等级->" + playerModel.getLevel() + " 战力->" + playerModel.getFc());
//                    break;
//                }
//            }
//        }

        WriteServerMapper writeServerMapper = SpringContext.getBean(WriteServerMapper.class);
        writeServerMapper.update(WorldInfoFactory.getInstance().save());

        System.out.println("修复成功----------------->");
    }


    public static void fixAllDB()
    {
        SpringServer.start();

        LoadManager.GMLoad();

        ReadServerMapper readServerMapper = SpringContext.getBean(ReadServerMapper.class);

        WorldInfoFactory.getInstance().load(readServerMapper.find());

        System.out.println("加载世界信息成功");

        ReadPlayerMapper playerMapper = SpringContext.getBean(ReadPlayerMapper.class);

        List<Player> playerList = loadPlayer(playerMapper);

        System.out.println("加载玩家信息成功, 玩家数量 -> " + playerList.size() + " 休息3秒......");

        System.out.println("1");

        TimeUtils.sleep(1000);

        System.out.println("2");

        TimeUtils.sleep(1000);

        System.out.println("3");

        WriteServerMapper writeServerMapper = SpringContext.getBean(WriteServerMapper.class);
        writeServerMapper.update(WorldInfoFactory.getInstance().save());

        System.out.println("写入世界信息完成");

        JdbcTemplate jdbcTemplate = SpringContext.getBean(JdbcTemplate.class);

        savePlayer(jdbcTemplate, playerList);

        System.out.println("写入玩家信息完成!");

    }

    public static void reloadAll()
    {
        SpringServer.start();

        LoadManager.GMLoad();

        ReadServerMapper readServerMapper = SpringContext.getBean(ReadServerMapper.class);
        WorldInfoFactory.getInstance().load(readServerMapper.find());

        System.out.println("加载世界信息成功");

        ReadPlayerMapper playerMapper = SpringContext.getBean(ReadPlayerMapper.class);

        List<Player> playerList = loadPlayer(playerMapper);

        System.out.println("加载成功,玩家数量 -> " + playerList.size());
    }


    /**
     * 执行此操作前请确保游戏主服务器是关闭的
     * 把所有玩家数据和世界数据备份到新的数据库,在新的数据库里进行以下操作
     * 1.loadFrom和writeTo方法里添加新字段,并且一一对应
     * 2.注释掉所有loadFrom方法里新加的字段
     * 3.存储所有玩家数据和世界数据
     * 4.把loadFrom方法里注释掉的取消注释
     * 5.再load一次玩家数据和世界数据,确保已经正确修复
     * 6.如果没有错误,则把数据替换到正式数据库
     * 7.如果有错,则从头再来
     */
    public static void fixWorldDB()
    {
        SpringServer.start();

        LoadManager.GMLoad();

        ReadServerMapper readServerMapper = SpringContext.getBean(ReadServerMapper.class);

        WorldInfoFactory.getInstance().load(readServerMapper.find());

        System.out.println("加载世界信息成功,休息3秒......");

        System.out.println("1");

        TimeUtils.sleep(1000);

        System.out.println("2");

        TimeUtils.sleep(1000);

        System.out.println("3");

        WriteServerMapper writeServerMapper = SpringContext.getBean(WriteServerMapper.class);
        writeServerMapper.update(WorldInfoFactory.getInstance().save());

        System.out.println("写入完成!");
    }

    public static void fixPlayerDB()
    {
        SpringServer.start();

        LoadManager.GMLoad();

        ReadPlayerMapper playerMapper = SpringContext.getBean(ReadPlayerMapper.class);

        List<Player> playerList = loadPlayer(playerMapper);

        System.out.println("加载成功, 玩家数量 -> " + playerList.size() + " 休息3秒......");

        JdbcTemplate jdbcTemplate = SpringContext.getBean(JdbcTemplate.class);

        System.out.println("1");

        TimeUtils.sleep(1000);

        System.out.println("2");

        TimeUtils.sleep(1000);

        System.out.println("3");

        savePlayer(jdbcTemplate, playerList);

        System.out.println("写入完成!");

    }


    public static List<Player> loadPlayer(ReadPlayerMapper playerMapper)
    {
        List<PlayerModel> playerModelList = playerMapper.findAll();
        Player player = null;

        List<Player> playerList = new CopyOnWriteArrayList<>();
        for(PlayerModel playerModel : playerModelList)
        {
            player = new Player(playerModel);

            playerList.add(player);
        }

        return playerList;
    }

    public static List<Player> loadRobot(ReadPlayerMapper playerMapper)
    {
        List<PlayerModel> playerModelList = playerMapper.findAllRobot();
        Player player = null;

        List<Player> playerList = new CopyOnWriteArrayList<>();
        for(PlayerModel playerModel : playerModelList)
        {
            player = new Player(playerModel);

            playerList.add(player);
        }

        return playerList;
    }


    public static void savePlayer(JdbcTemplate jdbcTemplate, List<Player> playerList)
    {
        String sql = "update player set fc = ?,bagInfo=?,challengeInfo=?,taskInfo=?,boonInfo=?,settingInfo=?,mailInfo=?,majorInfo=? where objectIndex=?";
        //批量更新玩家数据
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException
            {
                Player player = playerList.get(i);
                ps.setInt(1, player.fc);
                ps.setBytes(2, player.getExtInfo(BagInfo.class).getData());
                ps.setBytes(3, player.getExtInfo(ChallengeInfo.class).getData());
                ps.setBytes(4, player.getExtInfo(TaskInfo.class).getData());
                ps.setBytes(5, player.getExtInfo(BoonInfo.class).getData());
                ps.setBytes(6, player.getExtInfo(SettingInfo.class).getData());
                ps.setBytes(7, player.getExtInfo(MailInfo.class).getData());
                ps.setBytes(8, player.getExtInfo(MajorInfo.class).getData());
                ps.setString(9, player.getObjectIndex());
            }

            @Override
            public int getBatchSize() {
                return playerList.size();
            }
        });
    }

    public static void reloadWorldInfo()
    {
        SpringServer.start();

        LoadManager.GMLoad();

        ReadServerMapper readServerMapper = SpringContext.getBean(ReadServerMapper.class);
        WorldInfoFactory.getInstance().load(readServerMapper.find());

        System.out.println("重新加载Server成功");
    }

    public static void reloadPlayer()
    {
        SpringServer.start();

        LoadManager.GMLoad();

        ReadPlayerMapper playerMapper = SpringContext.getBean(ReadPlayerMapper.class);

        List<Player> playerList = loadPlayer(playerMapper);

        System.out.println("重新加载成功,玩家数量 -> " + playerList.size());

    }


//    public static void fixArena()
//    {
//        ReadServerMapper readServerMapper = SpringContext.getBean(ReadServerMapper.class);
//        ServerModel serverModel =  readServerMapper.find();
//        WorldInfoFactory worldInfo = WorldInfoFactory.getInstance();
//        worldInfo.load(serverModel);
//
//        List<WorldArenaBean> list = worldInfo.getArenaRankInfo().getArenaRankList();
//        System.out.println("arena ->" + list);
//
//        worldInfo.getArenaRankInfo().getArenaRankList().clear();
//        ReadPlayerMapper readPlayerMapper = SpringContext.getBean(ReadPlayerMapper.class);
//        List<PlayerModel> playerList = readPlayerMapper.findAll();
//        for(PlayerModel playerModel : playerList)
//        {
////            System.out.println("uid -> " + playerModel.getUid() + "  " + playerModel.getBean());
//            worldInfo.getArenaRankInfo().addArenaRankInfo(playerModel.getObjectIndex());
//        }
//
//        System.out.println(worldInfo.getArenaRankInfo().getArenaRankList().size());
//
//        WriteServerMapper writeServerMapper = SpringContext.getBean(WriteServerMapper.class);
//        writeServerMapper.update(worldInfo.save());
//
//
//        SpringContext.stop();
//
//    }
//
//
//    public static  void fixArena2(){
//        ReadServerMapper readServerMapper = SpringContext.getBean(ReadServerMapper.class);
//        ServerModel serverModel =  readServerMapper.find();
//        WorldInfoFactory worldInfo = WorldInfoFactory.getInstance();
//        worldInfo.load(serverModel);
//
//        List<WorldArenaBean> list = worldInfo.getArenaRankInfo().getArenaRankList();
//        System.out.println("竞技场数据修复之前：" + list);
//
//        List<String> ids = new ArrayList<>();
//        for(WorldArenaBean bean : list){
//            ids.add(bean.getObjectIndex());
//        }
//
//        ReadPlayerMapper mapper = SpringContext.getBean(ReadPlayerMapper.class);
//        List<PlayerModel> players = mapper.getPlayerListByIndex(ids);
//
//        int r = 0;
//        System.out.println("修复前------------------------------>");
//        for(WorldArenaBean bean : list)
//        {
//            System.out.print("名次：" + (++r));
//            for(PlayerModel model : players)
//            {
//                if(model.getObjectIndex().equals(bean.getObjectIndex()))
//                {
//                    System.out.println("  "+(model.getUid()==0?"我是机器人":"玩家->"+ model.getName()) + " UID:" + model.getUid() + " 战力:" + model.getFc());
//                    break;
//                }
//            }
//            if(r>=100) break;
//        }
//
//        Map<Integer, String> realPlayer = new HashMap<>();
//        List<PlayerModel> robot = new ArrayList<>();
//        //找出所有的玩家和位置
//        for(int i = 0; i < players.size(); i ++){
//            PlayerModel model = players.get(i);
//            if(model.getUid() == 0){
//                robot.add(model);
//            }else {
//                realPlayer.put(i, model.getObjectIndex());
//            }
//        }
//
//        Collections.sort(robot, new Comparator<PlayerModel>() {
//            @Override
//            public int compare(PlayerModel o1, PlayerModel o2) {
//                return new Integer(o2.getFc()).compareTo(o1.getFc());
//            }
//        });
//
//        int index = 0;
//        for(WorldArenaBean bean : list){
//            if(isRobot(players, bean.getObjectIndex())){
//                bean.setObjectIndex(robot.get(index++).getObjectIndex());
//            }
//        }
//
//        r = 0;
//        System.out.println("修复后------------------------------>");
//        for(WorldArenaBean bean : list)
//        {
//            System.out.print("名次：" + (++r));
//            for(PlayerModel model : players)
//            {
//                if(model.getObjectIndex().equals(bean.getObjectIndex()))
//                {
//                    System.out.println("  "+(model.getUid()==0?"我是机器人":"玩家->"+ model.getName()) + " UID:" + model.getUid() + " 战力:" + model.getFc());
//                    break;
//                }
//            }
//            if(r>=100) break;
//        }
//        System.out.println("竞技场数据修复之后：" + list);
//        WriteServerMapper writeServerMapper = SpringContext.getBean(WriteServerMapper.class);
//        writeServerMapper.update(worldInfo.save());
//        SpringContext.stop();
//    }
//
//    public static boolean isRobot(List<PlayerModel> players, String objectIndex){
//        for(PlayerModel model : players){
//            if(model.getObjectIndex().equals(objectIndex)){
//                return model.getUid() == 0;
//            }
//        }
//        return false;
//    }
//    public static void fixVipByPlayer()
//    {
//        SpringServer.start();
//
//        LoadFactory.loadGameObject("data/");
//
//        TimeUtils.sleep(1000);
//
//        ReadPlayerMapper readPlayerMapper = SpringContext.getBean(ReadPlayerMapper.class);
//        ReadUserMapper readUserMapper = SpringContext.getBean(ReadUserMapper.class);
//
//        List<PlayerModel> playerModelList = readPlayerMapper.findAll();
//
//        BufferedReader br = null;
//        InputStream input = null;
//        try {
//            input = LoadFactory.getInputStream("recharge.json");
//            br = new BufferedReader(new UnicodeReader(input));
//            String line = null;
//            StringBuilder data = new StringBuilder();
//            while((line = br.readLine()) != null) {
//                data.append(line);
//            }
//
//            JSONObject jsonObject = JSONObject.parseObject(data.toString());
//            JSONArray jsonArray = jsonObject.getJSONArray("RECORDS");
//
//            List<Player> playerList = new ArrayList<>();
//
//            for(int i = 0; i < jsonArray.size(); i++)
//            {
//                JSONObject object = jsonArray.getJSONObject(i);
//                long currentTime = System.currentTimeMillis();
//
//                String account = object.getString("account");
//                String[] ids = object.getString("rechargeId").split(",");
//
//                for(PlayerModel playerModel : playerModelList)
//                {
//                    if(StringUtils.isNullOrEmpty(playerModel.getAccount()) || playerModel.getAccount().startsWith("QUICK"))
//                        continue;
//
//                    if(playerModel.getAccount().equals(account))
//                    {
//                        Player player = new Player(playerModel);
//
//                        RechargeRecordData recordData = player.getRechargeRecordData();
//                        for(String idStr : ids)
//                        {
//                            int rechargeId = Integer.parseInt(idStr);
//                            RechargeConfig rechargeConfig = DataFactory.getInstance().getGameObject(DataFactory.RECHARGE_KEY, rechargeId);
//
//                            if(rechargeConfig.type == E_RechargeType.MOUTH_CARD.getCode()){
//                                RechargeRecordData.MouthCardRecord mouthCardRecord = recordData.getRechargeRecordMap().get(rechargeId);
//                                if(!recordData.hasRechargeRecord(rechargeId))
//                                {
//                                    recordData.addRecord(rechargeId, currentTime + ConstantFactory.DAY_TIME * rechargeConfig.days);
//                                    System.out.println(playerModel.getName() + "  11给他一张 " + rechargeConfig.rmb + " 的月卡, 当前时间 -> " + DataFactory.currentTime);
//                                }
//                                else  {
//                                    if(mouthCardRecord.getEndTime() < currentTime)
//                                    {
//                                        recordData.addRecord(rechargeId, currentTime + ConstantFactory.DAY_TIME * rechargeConfig.days);
//                                        System.out.println(playerModel.getName() + "  22给他一张 " + rechargeConfig.rmb + " 的月卡, 当前时间 -> " + DataFactory.currentTime);
//                                    }
//
//                                }
//                            }
//                        }
//
//                        playerList.add(player);
//                        break;
//                    }
//                }
//            }
//
//
//            PlayerService playerService = SpringContext.getBean(PlayerService.class);
//            playerService.updatePlayers(playerList, true, BoonInfo.class);
//
//        } catch (IOException e) {
//            e.printStackTrace();
//            LogUtils.error("充值返还json解析异常");
//        }
//
//        SpringContext.stop();
//        System.exit(0);
//    }
//
//
//    public static void loadPlayer()
//    {
//        SpringServer.start();
//
//        LoadFactory.loadGameObject("data/");
//
//        ReadPlayerMapper readPlayerMapper = SpringContext.getBean(ReadPlayerMapper.class);
//        List<PlayerModel> playerModelList = readPlayerMapper.findAll();
//
//        List<Compensate> list = LoadFactory.compensateList();
//
//        List<Player> playerList = new ArrayList<>();
//        long currentTime = System.currentTimeMillis();
//        for(int i = 0 ; i < list.size(); i++)
//        {
//            String account = list.get(i).getAccount();
//            int money = list.get(i).getMoney();
//
//            for(PlayerModel playerModel : playerModelList)
//            {
//                if(StringUtils.isNullOrEmpty(playerModel.getAccount()) || playerModel.getAccount().startsWith("QUICK"))
//                    continue;
//
//                if(playerModel.getAccount().equals(account))
//                {
//                    System.out.println("玩家 -> " + playerModel.getName() + " 充值 -> " + money);
//                    Player player = new Player(playerModel);
//                    fixVip(player, money, currentTime);
//                    playerList.add(player);
//                    break;
//                }
//            }
//        }
//
//        PlayerService playerService = SpringContext.getBean(PlayerService.class);
//        playerService.updatePlayers(playerList, true, BoonInfo.class);
//    }

    public static void fixVip(Player player, int money, long currentTime)
    {
        player.addVipExp(money*10);
        System.out.println("给他添加VIP经验 -> " + (money*10));

        if(money >= 80)
        {
            fixMoneyCard(player, 1, currentTime);
            fixMoneyCard(player, 2, currentTime);
        }
        else if(money >= 50)
        {
            fixMoneyCard(player, 2, currentTime);
        }
        else if(money >= 30)
        {
            fixMoneyCard(player, 1, currentTime);
        }
    }

    private static void fixMoneyCard(Player player, int rechargeId, long currentTime)
    {
        RechargeRecordData recordData = player.getRechargeRecordData();
        RechargeConfig rechargeConfig = DataFactory.getInstance().getGameObject(DataFactory.RECHARGE_KEY, rechargeId);
        System.out.println("给他一张 " + rechargeConfig.rmb + " 的月卡, 当前时间 -> " + DataFactory.currentTime);
        if(rechargeConfig.type == E_RechargeGoodsType.MOUTH_CARD.getCode()){
            if(recordData.hasRechargeRecord(rechargeId)) {//曾经购买过
                long endTime = recordData.getRechargeRecordMap().get(rechargeId).getEndTime();
                long max = Math.max(currentTime, endTime);
                recordData.addRecord(rechargeId, max + ConstantFactory.DAY_TIME * rechargeConfig.days);
            }else {
                recordData.addRecord(rechargeId, currentTime + ConstantFactory.DAY_TIME * rechargeConfig.days);
            }
        }
    }

    public static void fixMergeArea(Player player)
    {
        //开服基金处理
        BonusData bonusData = player.getBonusData();
        List<Integer> fundRecord;

        if(bonusData.isFundBuy(SGCommonProto.E_FUND_TYPE.FUND_TYPE_OPEN)){
            fundRecord = bonusData.getFundReceiverRecord(SGCommonProto.E_FUND_TYPE.FUND_TYPE_OPEN);
            List<FundRewardConfig> configs = DataFactory.getInstance().getDataList(DataFactory.FUND_REWARD_KEY);
            Map<Integer, Integer> map = new TreeMap<>();
            for(FundRewardConfig config : configs){
                if(config.type == SGCommonProto.E_FUND_TYPE.FUND_TYPE_OPEN_VALUE){
                    if(!fundRecord.contains(config.id)){
                        map.put(config.minlv, config.reward[0].value);
                    }
                }
            }

            bonusData.clearFund(SGCommonProto.E_FUND_TYPE.FUND_TYPE_OPEN);
            if(map.size() > 0){
                StringBuffer buffer = new StringBuffer();
                buffer.append("由于基金功能调整，我们将返还充值后未领取的所有档次的基金，返还后您将可以再次进行购买\n" +
                        "您未领取的开服基金有：\n");
                Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator();
                int sum = 0;
                while(iterator.hasNext()){
                    Map.Entry<Integer, Integer> next = iterator.next();
                    buffer.append("等级达到"+ next.getKey() +" : " + next.getValue() + "钻石\n");
                    sum+=next.getValue();
                }
                buffer.append("总计:" + sum + "钻石\n");
                buffer.append("感谢您对我们游戏的支持");
                List<GoodsData> datas = new ArrayList<>();
                datas.add(new GoodsData(SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_ASSETS_VALUE, ConfigFactory.ASSET_MONEY_KEY, sum));
                MailDB mailDB = new MailDB("开服基金调整", buffer.toString(), datas);
                MailInfo mailInfo = player.getExtInfo(MailInfo.class);
                mailInfo.addMail(SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_SYSTEM_VALUE, mailDB);

                LogUtils.info("开服基金处理,玩家 -> name:" + player.getName() + " uid:" + player.getUid() + " index:" + player.getObjectIndex());
            }


        }

        //新区基金处理
        if(bonusData.isFundBuy(SGCommonProto.E_FUND_TYPE.FUND_TYPE_NEW_SERVER)){
            //领取记录,若没有购买则没有记录
            fundRecord = bonusData.getFundReceiverRecord(SGCommonProto.E_FUND_TYPE.FUND_TYPE_NEW_SERVER);
            List<FundRewardConfig> configs = DataFactory.getInstance().getDataList(DataFactory.FUND_REWARD_KEY);
            Map<Integer, Integer> map = new TreeMap<>();
            int minDays = player.getBonusData().getLoginTimesByDayForNewFund();//天数
            for(FundRewardConfig config : configs){
                if(config.type == SGCommonProto.E_FUND_TYPE.FUND_TYPE_NEW_SERVER_VALUE){
                    //去掉天数条件 全部都给
                    if(!fundRecord.contains(config.id)){
                        map.put(config.minlv, config.reward[0].value);
                    }
                }
            }
            bonusData.clearFund(SGCommonProto.E_FUND_TYPE.FUND_TYPE_NEW_SERVER);
            if(map.size() > 0){
                StringBuffer buffer = new StringBuffer();
                buffer.append("由于基金功能调整，我们将返还充值后未领取的所有档次的基金，返还后您将可以再次进行购买\n" +
                        "您未领取的新区基金有：\n");
                Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator();
                int sum = 0;
                while(iterator.hasNext()){
                    Map.Entry<Integer, Integer> next = iterator.next();
                    buffer.append("登录天数"+ next.getKey() +" : " + next.getValue() + "钻石\n");
                    sum+=next.getValue();
                }
                buffer.append("总计:" + sum + "钻石\n");
                buffer.append("感谢您对我们游戏的支持");
                List<GoodsData> datas = new ArrayList<>();
                datas.add(new GoodsData(SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_ASSETS_VALUE, ConfigFactory.ASSET_MONEY_KEY, sum));
                MailDB mailDB = new MailDB("新区基金调整", buffer.toString(), datas);
                MailInfo mailInfo = player.getExtInfo(MailInfo.class);
                mailInfo.addMail(SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_SYSTEM_VALUE, mailDB);

                LogUtils.info("新区基金处理,玩家 -> name:" + player.getName() + " uid:" + player.getUid() + " index:" + player.getObjectIndex());
            }
        }

    }



    public static void auctionPlayerUpdate(Map<String,AuctionRewardPlayer> savePlayerMap){
        if(savePlayerMap.size() > 0){
            //遍历map
            List<Player> savePlayerList = new ArrayList<>();
            TwoTuple<Player,PlayerController> twoTuple;
            Player player;
            for(Map.Entry<String,AuctionRewardPlayer> entry:savePlayerMap.entrySet()){
                twoTuple =  GameServer.getInstance().getWorldManager().getPlayerResult(entry.getKey());

                player = twoTuple.getFirst();
                if(player == null){
                    continue;
                }
                player.getBonusData().addAuctionReward(entry.getValue().getTotalRewardMoney());
                if(entry.getValue().getRewardMailList() != null && entry.getValue().getRewardMailList().size() > 0){
                    for (MailDB mailDB:entry.getValue().getRewardMailList()){
                        MailInfo mailInfo = twoTuple.getFirst().getExtInfo(MailInfo.class);
                        mailInfo.addMail(SGCommonProto.E_MAIL_TYPE.MAIL_TYPE_SYSTEM_VALUE, mailDB);
                    }
                }

                if(twoTuple.getSecond() == null) {
                    savePlayerList.add(player);
                }
            }
            if(savePlayerList.size() > 0){
                GameServer.getInstance().savePlayerListSync(savePlayerList, false, BoonInfo.class, MailInfo.class);
            }
        }
    }


}
